package com.linxi.handy.component.tab;

import javax.swing.*;
import javax.swing.plaf.basic.BasicButtonUI;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

/**
 * 按钮选项卡组件
 *
 * @author user
 */
public class ButtonTabComponent extends JPanel {
    private final JTabbedPane pane;

    public ButtonTabComponent(final JTabbedPane pane) {
        //取消设置默认 FlowLayout 的间隙
        super(new FlowLayout(FlowLayout.LEFT, 0, 0));
        if (pane == null) {
            throw new NullPointerException("TabbedPane is null");
        }
        this.pane = pane;
        setOpaque(false);
        //让 JLabel 从 JTabbedPane 读取标题
        JLabel label = new JLabel() {
            @Override
            public String getText() {
                int i = pane.indexOfTabComponent(ButtonTabComponent.this);
                if (i != -1) {
                    return pane.getTitleAt(i);
                }
                return null;
            }
        };
        add(label);
        //在标签和按钮之间添加更多空间
        label.setBorder(BorderFactory.createEmptyBorder(0, 0, 0, 5));
        //选项卡按钮
        JButton button = new TabButton();
        add(button);
        //在组件顶部添加更多空间
        setBorder(BorderFactory.createEmptyBorder(2, 0, 0, 0));
    }

    private class TabButton extends JButton implements ActionListener {
        private static final long serialVersionUID = 1L;

        public TabButton() {
            int size = 17;
            setPreferredSize(new Dimension(size, size));
            setToolTipText("close this tab");
            setUI(new BasicButtonUI());
            setContentAreaFilled(false);
            setFocusable(false);
            setBorder(BorderFactory.createEtchedBorder());
            setBorderPainted(false);
            setRolloverEnabled(true);
            addActionListener(this);
        }

        @Override
        public void actionPerformed(ActionEvent e) {
            int i = pane.indexOfTabComponent(ButtonTabComponent.this);
            if (i != -1) {
                pane.remove(i);
            }
        }

        /**
         * 我们不想更新此按钮的 UI
         */
        @Override
        public void updateUI() {
        }

        /**
         * 画十字架
         *
         * @param g
         */
        @Override
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);
            Graphics2D g2 = (Graphics2D) g.create();
            //移动按下按钮的图像
            if (getModel().isPressed()) {
                //将 Graphics2D 上下文的原点平移到当前坐标系统中的点 (x, y)。
                g2.translate(1, 1);
            }
            g2.setStroke(new BasicStroke(3));
            g2.setColor(new Color(255, 255, 255, 100));
            if (getModel().isRollover()) {
                g2.setColor(new Color(252, 160, 160));
            }
            int delta = 5;
            g2.drawLine(delta, delta, getWidth() - delta, getHeight() - delta);
            g2.drawLine(getWidth() - delta, delta, delta, getHeight() - delta);
            g2.dispose();
        }
    }
}